<?php

/* 
 * 代码model
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class CodeModel extends db_help
{
    public $keyFields = 'code_id';//设置主键
    public function __construct() {
        parent::__construct();
        $this->table_name = $this->table('code');
    }
    public static function load(){
        return new self();
    }
    public function get_list() {
        return $this->getAll();
    }
    public function get_game_name($code_id) {
        return $this->getOne('code_name', array('code_id' => $code_id));
    }

    /**
     * 更新最后一次访问时间
     * @param int $code_id 代码ID
     * @return type
     */
    public function update_last_time($code_id) {
        $cTime = p_getTime();
        $total_count = 0;
        $count = $this->getOne('visit_count', array('code_id' => $code_id));
        if($count > 0) {
            $total_count = intval($count) + 1;            
        }
        $sql = "update {$this->table_name} set last_time = {$cTime},visit_count = {$total_count} where code_id = '{$code_id}'";
        return $this->query($sql);
    }    

    /**
     * 判斷是否重復appid
     * @param type $appid
     * @return type
     */
    public function check_appid($appid) {
        $where = array(
            'appid' => $appid,
        );
        return $this->getOne('code_id', $where);
    }
    /**
     * 判斷是否重復代碼名稱
     * @param type $code_name
     * @return type
     */
    public function check_code_user($code_name) {
        $where = array(
            'code_name' => $code_name,
        );
        return $this->getOne('code_id', $where);
    }
    /**
     * 新增數據
     * @param type $data
     * @return type
     */
    public function in_data($data) {
        $bool = $this->insert($data);
        return $bool;
    }
    /**
     * 以APPID獲取信息
     * @param type $appid
     * @return type
     */
    public function get_infoByAppid($appid) {
        $where = array(
            'appid' => $appid,
        );
        return $this->getRow(null, $where);
    }
}

